/**
 * <p>Copyright: Copyright (c) 2012</p>
 * <p>Company: �������ƿƼ����޹�˾</p>
 * <p>Aug 8, 2012 2:41:50 PM</p>
 * @author wangyunpeng
 * @version 1.0
 */
package com.ce.study.socketpool;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;

import com.ce.utils.Formatter;

/** 
 * desc:
 * <p>�����ˣ�wangyunpeng �������ڣ�Aug 8, 2012 </p>
 * @version V1.0  
 */
public class CM20JAVA extends CM20MessageBase{

	public CM20JAVA(Class<?> cls) {
		super(cls);
	}

	public CM20JAVA(Map<String, String> mbody) {
		super("JAVA", mbody);
	}

	public void encode() throws Exception {
		if(this.mbody == null) {
			log.debug("mbody is null!");
			throw new Exception("mbody is null!");
		}
		
		ByteArrayOutputStream baout = new ByteArrayOutputStream();
		ObjectOutputStream oout = new ObjectOutputStream(baout);
		oout.writeObject(this.mbody);
		oout.flush();
		oout.close();
		baout.close();
		this.bbody = baout.toByteArray();
		this.bodyLen = this.bbody.length;
		LEN_8 = Formatter.ralign(String.valueOf(bodyLen+fixLen), 8, '0');
	}

	public Object getBodyObject() throws Exception {
		if(this.bbody == null || this.bbody.length == 0) {
			log.debug("bbody is null or empty!");
			throw new Exception("bbody is null or empty!");
		}
		ByteArrayInputStream bain = new ByteArrayInputStream(this.bbody);
		ObjectInputStream oin = new ObjectInputStream(bain);
		Object obj = oin.readObject();
		oin.close();
		bain.close();
		return obj;
	}

}
